trigger trg_SuggestionComplain on Suggestion_and_Complaints__c (after update,after insert)
{
    Set<ID> scObjectIds = new Set<ID>();
    Map<String, decimal> slaHoursMap = new Map<String, decimal>();
    List<SLA__c> slaList = new List<SLA__c>();
    
    
    if(Trigger.isInsert && Trigger.isAfter)
    {
    
        //For SLA Hours...
        for(SLA_Custom_Setting__c slaRecord : SLA_Custom_Setting__c.getAll().values())
        {
            if(slaRecord.Process_Type__c == 'Case')
            {
                slaHoursMap.put(slaRecord.Application_Type__c, slaRecord.SLA_Hours__c );
            }
                                    
        }
        
        if(slaHoursMap.size() > 0)
        {
            // Assumes the BSC work hours are the default for the Org
            List<BusinessHours> businessHoursObjectList = new List<BusinessHours>([select id from businesshours where isDefault = true LIMIT 1]) ;

            if(businessHoursObjectList.size() > 0)
            {
                for(Suggestion_and_Complaints__c scObject : Trigger.New)
                {
                     if(slaHoursMap.get(scObject.Type__c) != null)
                     {
                         SLA__c slaObject = new SLA__c();
                         slaObject.SLA_Hours__c = slaHoursMap.get(scObject.Type__c);
                         slaObject.Actual_Start_Date_Time__c = DateTime.Now();
                   
                         // BSC works 11.5 hours / day (8:00 AM - 7:30 PM, M-F). Our SLA is 2-days (23 business hours)
                         System.debug('The Actual Start Time => '+ slaObject.Actual_Start_Date_Time__c );
                         System.debug('The Expected End Date => '+ slaObject.Actual_Start_Date_Time__c.addhours(integer.valueof(slaHoursMap.get(scObject.Type__c))));
                         System.debug('The Business Hour Date => ' + BusinessHours.add(businessHoursObjectList[0].id, slaObject.Actual_Start_Date_Time__c, integer.valueof(slaHoursMap.get(scObject.Type__c)) * 60 * 60 * 1000L));
                         System.debug('The Business Hour Date GMT => ' + BusinessHours.addGmt(businessHoursObjectList[0].id, slaObject.Actual_Start_Date_Time__c, integer.valueof(slaHoursMap.get(scObject.Type__c)) * 60 * 60 * 1000L));
                         slaObject.Expected_End_Date__c = BusinessHours.addGmt(businessHoursObjectList[0].id, slaObject.Actual_Start_Date_Time__c, integer.valueof(slaHoursMap.get(scObject.Type__c)) * 60 * 60 * 1000L); 
                         slaObject.Suggestion_and_Complaint__c = scObject.Id ;
                         slaObject.Process_Type__c = 'Case';
                         slaObject.Application_Type__c = scObject.Type__c;
                         //Adding in the List...
                         slaList.add(slaObject);
                     
                     
                     }    
                    
                }
                
                //For SLA...
                if(slaList.size() > 0)
                    upsert slaList;
            }            
        
        }
        
        
    }
    
    if(Trigger.isUpdate && Trigger.isAfter)
    {
        for(Suggestion_and_Complaints__c scObject : Trigger.New)
        {
            if(Trigger.oldMap.get(scObject.Id).Status__c != scObject.Status__c)    
            {
                if(scObject.Status__c == 'Closed' || scObject.Status__c == 'Rejected' )
                    scObjectIds.add(scObject.Id);    
            
            }
        }
    
    
        if(scObjectIds.size() > 0)
        {
            
        
            for(SLA__c slaObject : [SELECT ID, isCompleted__c, Actual_End_Date_Time__c, Actual_Start_Date_Time__c, Amendment__c, Application_Type__c, Expected_End_Date__c, License__c,isCancelled__c, Process_Type__c,Suggestion_and_Complaint__c, SLA_Hours__c, Status__c, Tenancy_Contract__c FROM SLA__c where Suggestion_and_Complaint__c IN: scObjectIds AND isCompleted__c =: false AND isCancelled__c =: false ORDER BY CreatedDate])
            {
            
                //Putting the Actual_End_Date_Time__c End Date of SLA...
                slaObject.Actual_End_Date_Time__c = DateTime.Now();
                slaObject.isCompleted__c =true;
                //Adding in the List...
                slaList.add(slaObject);
            }
            
            //Upserting the SLA List...
            if(slaList.size() > 0)
                upsert slaList;
        
        
        }
        
    
    }
    
}